//
// Created by whg on 2025-06-17.
//

#ifndef PYVM_2025_FRAME_OBJECT_HPP
#define PYVM_2025_FRAME_OBJECT_HPP


#include "code/codeObject.hpp"
#include "util/map.hpp"
#include "functionObject.hpp"

class FrameObject {
public:
    // 用于创建第一个栈帧
    FrameObject(CodeObject* code);
    // 用于创建函数调用的栈帧
    FrameObject(FunctionObject* func, ObjList args);
    ~FrameObject() {};

private:
    CodeObject* _code;
    int _pc;

    ArrayList<PyObject*>* _stack;
    PyList* _consts;
    PyList* _names;
    Map<PyObject*, PyObject*>* _locals;
    Map<PyObject*, PyObject*>* _globals;
    ArrayList<PyObject*>* _fastLocals;

    FrameObject* _nextFrame;

    void init(CodeObject *code);

public:
    void setPc(int pc){
        _pc = pc;
    }
    int getPc(){
        return _pc;
    }

    ArrayList<PyObject*>* stack(){
        return _stack;
    }
    PyList* consts(){
        return _consts;
    }
    PyList* names(){
        return _names;
    }
    Map<PyObject*, PyObject*>* locals(){
        return _locals;
    }
    Map<PyObject*, PyObject*>* globals(){
        return _globals;
    }
    ArrayList<PyObject*>* fastLocals(){
        return _fastLocals;
    }

    void setNextFrame(FrameObject* nextFrame){
        _nextFrame = nextFrame;
    }
    FrameObject* nextFrame(){
        return _nextFrame;
    }

    unsigned char getOpCode();
    int getOpArg();
    bool hasMoreCode();
};


#endif //PYVM_2025_FRAME_OBJECT_HPP
